﻿using B.S.DataBase.MyProject.Domain.Procurements;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.ProcurementsCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.ProcurementsCommandCommand
{
    /// <summary>
    /// 修改开票通知单的所有信息处理方法
    /// </summary>
    public class UdpInvoiceSettlementEntityCommandHandler:IRequestHandler<UdpInvoiceSettlementEntityCommand,APIResult<int>>
    {
        private readonly IBaseRepository<InvoiceSettlementEntity> iRepo;
        private readonly IBaseRepository<OrderMaterials> oRepo;

        public UdpInvoiceSettlementEntityCommandHandler(IBaseRepository<InvoiceSettlementEntity>iRepo,IBaseRepository<OrderMaterials>oRepo)
        {
            this.iRepo = iRepo;
            this.oRepo = oRepo;
        }
        /// <summary>
        /// 处理方法
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<APIResult<int>> Handle(UdpInvoiceSettlementEntityCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var list = iRepo.GetAll().Result.Where(x=>x.InvoiceSettlementEntityId == request.InvoiceSettlementEntityId).FirstOrDefault();
            list.InvoiceSettlementEntityId = request.InvoiceSettlementEntityId;
            list.DocumentCode = request.DocumentCode;
            list.BusinessType = request.BusinessType;
            list.CustomerName = request.CustomerName;
            list.Bank = request.Bank;
            list.DeliveryEmail = request.DeliveryEmail;
            list.DocumentDate = request.DocumentDate;
            list.Operator = request.Operator;
            list.InvoiceTypeFlag = request.InvoiceTypeFlag;
            list.AccountNumber = request.AccountNumber;
            list.Remark = request.Remark;
            list.DocumentStatus = request.DocumentStatus;
            list.Auditor = request.Auditor;
            list.TaxRate = request.TaxRate;
            list.TaxNumber = request.TaxNumber;
            list.BusinessStatus = request.BusinessStatus;
            list.AuditDate = request.AuditDate;
            list.InvoiceType = request.InvoiceType;
            list.AddressPhone = request.AddressPhone;
            var r = iRepo.Update(list).Result;
            if(r>0)
            {
                var or = oRepo.GetAll().Result.Where(x => x.SaleCode == request.DocumentCode).FirstOrDefault();
                foreach (var item in request.OrderMaterials)
                {
                    or.MaterialsCode = item.MaterialsCode;
                    or.MaterialsName = item.MaterialsName;
                    or.ModelSpecification = item.ModelSpecification;
                    or.Calulate = item.Calulate;
                    or.CalulateNumber = item.CalulateNumber;
                    or.Price = item.Price;
                    or.TotalPrice = item.TotalPrice;
                    or.QualityRequir = item.QualityRequir;
                    or.PackRequir = item.PackRequir;
                }
               var g = oRepo.Update(or).Result;
                if(g>0)
                {
                    result.Code = APIEnum.修改成功;
                    result.Msg = "更改成功";
                    result.data = g;
                    return Task.FromResult(result);
                }


            }
            else
            {
                result.Code = APIEnum.修改失败;
                result.Msg = "更改失败";
                return Task.FromResult(result);
            }
            return Task.FromResult(result);



        }



    }
}
